﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;

namespace Fotofly.SilverlightPhotoViewerWebsite
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {
        [WebMethod]
        public PhotoMetadata GetMetadata(string filename)
        {
            string photoFile = HttpContext.Current.Server.MapPath(filename);
            string xmlFile = photoFile.Replace("jpg", "xml");

            if (File.Exists(photoFile))
            {
                JpgPhoto photo = new JpgPhoto();

                if (!File.Exists(xmlFile))
                {
                    photo = new JpgPhoto(photoFile);
                    photo.ReadMetadata();
                    photo.WriteMetadataToXml(xmlFile);
                }

                photo.ReadMetadataFromXml(xmlFile);

                return photo.Metadata;
            }
            else
            {
                return null;
            }
        }
    }
}
